package com.jlastu.androidsx.ui.user;

import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.jlastu.androidsx.App;
import com.jlastu.androidsx.R;
import com.jlastu.androidsx.adapter.HarvestAddressAdapter;
import com.jlastu.androidsx.adapter.OnAddressListener;
import com.jlastu.androidsx.bean.ShopAddress;
import com.jlastu.androidsx.database.ShopAddressDBUtils;
import com.jlastu.androidsx.databinding.ActivityShopAddressListBinding;
import com.jlastu.androidsx.widget.MyItemDecoration;

import java.util.List;

import cn.project.base.baseui.BaseNormalActivity;

public class ShopAddressListActivity extends BaseNormalActivity<ActivityShopAddressListBinding>  implements OnAddressListener {

    HarvestAddressAdapter harvestAddressAdapter;
    private List<ShopAddress> addressList;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop_address_list;
    }

    @Override
    protected void initView() {
        mBinding.toolbar.rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mBinding.toolbar.tvTitle.setText("收获地址");
        addressList = ShopAddressDBUtils.getInstance(getApplicationContext()).getListShopAddress(App.getInstance().user.getUser_id());
        if (addressList.size() == 0) {//没有收获地址显示无收获地址背景
            mBinding.rlEmpty.setVisibility(View.VISIBLE);
        }
        mBinding.swiper.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mBinding.swiper.setRefreshing(false);
            }
        });

        harvestAddressAdapter = new HarvestAddressAdapter(this);

        mBinding.rlAddress.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rlAddress.addItemDecoration(new MyItemDecoration(1));
        mBinding.rlAddress.setAdapter(harvestAddressAdapter);
        harvestAddressAdapter.setNewData(addressList);
        harvestAddressAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initData() {

        mBinding.tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(ShopAddressListActivity.this, AddAddressActivity.class),101);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        addressList.clear();
        addressList = ShopAddressDBUtils.getInstance(getApplicationContext()).getListShopAddress(App.getInstance().user.getUser_id());
        harvestAddressAdapter.setNewData(addressList);
        harvestAddressAdapter.notifyDataSetChanged();
    }

    @Override
    public void onEventMainThread(String event) {
        super.onEventMainThread(event);
        if ("添加地址".equals(event)){
            addressList.clear();
            addressList = ShopAddressDBUtils.getInstance(getApplicationContext()).getListShopAddress(App.getInstance().user.getUser_id());
            harvestAddressAdapter.setNewData(addressList);
            harvestAddressAdapter.notifyDataSetChanged();
        }
        harvestAddressAdapter.setNewData(addressList);
        harvestAddressAdapter.notifyDataSetChanged();
    }

    @Override
    public void onAddressClick(ShopAddress shopAddress) {
        Intent intent = new Intent(ShopAddressListActivity.this,HarvestAddressDetailActivity.class);
        intent.putExtra("detail",shopAddress);
        startActivity(intent);
    }
}
